import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import NCBackBtn from "src/hrpub/common/components/hr-back";
import {chectFormAction} from "../../../../../public/functions/checkFormAction";

class ContractDetail extends Component {
    constructor(props) {
        super(props);
        this.pageQueryClick = this.pageQueryClick.bind(this);
        this.goBack = this.goBack.bind(this);
        this.onAfterAddFormEvent = this.onAfterAddFormEvent.bind(this);
        this.handleEditBefore = this.handleEditBefore.bind(this);
    }

    componentDidMount() {
        if (this.props.data) {
            /*this.props.cardPagination.getNextCardPaginationId({
                id: this.props.data.rows[0].values.pk_psndoc_sub.value,
                status: 1
            });*/
            this.props.cardPagination.setCardPaginationId({
                id: this.props.data.rows[0].values.pk_psndoc_sub.value,
                status: 1
            });
        }
    }

    pageQueryClick(props, key) {
        const record = props.table.getAllTableData('contractList').rows.find(row => row.values.pk_psndoc_sub.value === key);
        this.props.view('', record.values)
    }

    goBack() {
        this.props.updateState({
            contractPage: 'main'
        }, () => {
            this.props.updateButton();
        })
    }

    handleEditBefore(props, moduleId, key, value, data) {
        let {areaCode, orgVal} = this.props;
        if (key === 'pk_conttext') {
            let meta = props.meta.getMeta();
            let editArea = areaCode;
            const formrelation = meta.formrelation && meta.formrelation[areaCode];
            if (formrelation) {
                editArea = formrelation[formrelation.length - 1];
            }
            meta[editArea].items.find(item => item.attrcode === key).queryCondition = function () {
                return {
                    "pk_org": orgVal.refpk,
                    "GridRefActionExt": "nccloud.web.hrcm.make.sqlbuilder.ContModelGridSqlBuilder"
                }
            };
            props.meta.setMeta(meta);
        }
        return true;
    }

    onAfterAddFormEvent(props, moduleId, key, value) {
        //handleAfter.call(this, moduleId, key, value);
        const {areaCode, appCode} = this.props;
        let editArea = areaCode;
        const formrelation = this.props.meta.getMeta().formrelation && this.props.meta.getMeta().formrelation[areaCode];
        if (formrelation) {
            editArea = formrelation[formrelation.length - 1];
        }
        chectFormAction(key, areaCode, props, [editArea], appCode)
    }

    render() {
        const {form, cardPagination, page, areaCode} = this.props;
        const {createCardPagination} = cardPagination;
        return (
            <div>
                {/*<NCAffix>*/}
                <div className="header" style={{display: page === 'view' ? '' : 'none'}}>
                    <NCBackBtn onClick={this.goBack}/>
                    {createCardPagination({handlePageInfoChange: this.pageQueryClick})}
                </div>
                {/*</NCAffix>*/}
                {areaCode ? form.createForm(areaCode, {
                    onAfterEvent: this.onAfterAddFormEvent,
                    onBeforeEvent: this.handleEditBefore
                }) : null}
            </div>
        );
    }
}

export default ContractDetail;
